import java.sql.*;

public class UserDao {

    //1.查询用户名和密码
    public User findByUsernameAndPassword(String username, String password) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;


        try {

            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            //连接数据库
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/cake_db?characterEncoding=utf-8",
                    "root", "dddd");

            //数据操作
            String sql = "select * from tbl_user where username = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, username);
            resultSet = statement.executeQuery();

            //只根据用户名找即可
            if (resultSet.next()) {
                User user = new User();
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setRealname(resultSet.getString("realname"));
                user.setState(resultSet.getInt("state"));
                user.setFailcount(resultSet.getInt("failcount"));
                return user;
            }


        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                    if (statement != null)
                        statement.close();
                    if (connection != null)
                        connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }


        return null;

    }


    //2.修改用户状态
    public void updateState(String username, int failCount) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;


        try {

            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            //连接数据库
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/cake_db?characterEncoding=utf-8",
                    "root", "dddd");

            //数据操作
            //改错误登录次数
            String sql = "update tbl_user set failcount = ? where username = ?";
            int count = failCount+1;
            statement = connection.prepareStatement(sql);
            statement.setInt(1, count);
            statement.setString(2, username);
            statement.executeUpdate();

            //改状态位
            String sql1 = "update tbl_user set state = 2 where username = ?";
            statement = connection.prepareStatement(sql1);
            statement.setString(1,username);
            statement.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }
}